<!-- src/views/user/AclEdit.vue -->
<script setup>
import { ref, reactive } from 'vue';
import { useAclStore } from '@/stores/aclStore.js';

const aclStore = useAclStore();

const dialogVisible = ref(false);

const acl = reactive({
  aclId: '',
  name: '',
  code: '',
  description: '',
  type: null,
  url: '',
  parentId: null,
 // isDelete: 0
});

const rules = {
  name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入权限代码', trigger: 'blur' }]
};

const aclForm = ref(null);

const submitForm = () => {


  aclForm.value?.validate().then( () => {
    console.log("校验通过", acl.value);

console.log("acl",acl)
    aclStore.updateAcl({
      ...acl
    })

  }).catch( () => {
    console.log("校验失败");
  })
  console.log("权限信息:", acl);

  close();

}



const show = (aclInfo) => {
  console.log("校验通过", aclInfo); // 检查 roleInfo 是否包含 id
  Object.assign(acl, aclInfo);
  acl.aclId =aclInfo.id;
  console.log("权限信息:", acl); // 检查 role 是否正确接收 id
  dialogVisible.value = true;
};

const close = () => {
  dialogVisible.value = false;

};

// 对外暴露show方法
defineExpose({
  show
});
</script>

<template>
  <el-dialog
      title="编辑权限"
      v-model="dialogVisible"
      width="50%"
  >
    <el-form :model="acl" :rules="rules" ref="aclForm" label-width="100px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="acl.name" placeholder="请输入权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限代码" prop="code">
        <el-input v-model="acl.code" placeholder="请输入权限代码"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type" :rules="[{ required: true, message: '请选择类型', trigger: 'change' }]">
        <el-select v-model="acl.type" placeholder="请选择类型">
          <el-option label="接口" :value="1"></el-option>
          <el-option label="菜单" :value="2"></el-option>
          <el-option label="按钮" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资源路径" prop="url">
        <el-input v-model="acl.url" placeholder="请输入资源路径"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submitForm">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped>
</style>
